package com.lanling.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.*;
import com.lanling.vod.other.ZhenhuaException;
import com.lanling.vod.service.VodService;
import com.lanling.vod.utils.ConstantVodUtils;
import com.lanling.vod.utils.InitVodCilent;
import org.springframework.stereotype.Service;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 胡振华
 * @date 2020-11-16 14:25
 */
@Service
public class VodServiceImpl implements VodService {

    //上传视频到阿里云
    @Override
    public Map uploadVideo(MultipartFile file) {
        try{
            String filename = file.getOriginalFilename();
            String title = filename.substring(0,filename.lastIndexOf('.'));
            String fileName = filename;
            InputStream inputStream = file.getInputStream();
            UploadStreamRequest request = new UploadStreamRequest(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET, title, fileName, inputStream);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            String videoId = response.getVideoId();


            DefaultAcsClient client = InitVodCilent.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建获取视频地址request和response
            GetPlayInfoRequest videoRequest = new GetPlayInfoRequest();
            GetPlayInfoResponse videoResponse = new GetPlayInfoResponse();
            //向request对象中设置视频Id
            videoRequest.setVideoId(videoId);
            //调用初始化对象里面的方法，传递request，获取数据
            videoResponse = client.getAcsResponse(videoRequest);
            List<GetPlayInfoResponse.PlayInfo> playInfoList = videoResponse.getPlayInfoList();
            String videoAddress = null;
            for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
                videoAddress = playInfo.getPlayURL();
            }
            Map<String,Object> map = new HashMap();
            map.put("videoId",videoId);
            map.put("videoAddress",videoAddress);
            return map;
        }catch (Exception e){
            e.printStackTrace();
            throw new ZhenhuaException(20001,"上传视频失败");
        }
    }

    //删除多个阿里云视频
    @Override
    public void removeMoreAlyVideo(List<String> videoIdList) {
        try {
            //初始化对象
            DefaultAcsClient client = InitVodCilent.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();

            //videoIdList值转换成 1,2,3
            String videoIds = StringUtils.join(videoIdList.toArray(), ",");

            //向request设置视频id
            request.setVideoIds(videoIds);
            //调用初始化对象的方法实现删除
            client.getAcsResponse(request);
        }catch(Exception e) {
            e.printStackTrace();
            throw new ZhenhuaException(20001,"删除视频失败");
        }
    }

    //根据视频id获取课程id和章节id
    @Override
    public Map<String, Object> getPlayAuth(String id) {
        try{
            DefaultAcsClient defaultAcsClient = InitVodCilent.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            request.setVideoId(id);
            GetVideoPlayAuthResponse response = defaultAcsClient.getAcsResponse(request);
            String playAuth = response.getPlayAuth();

            Map<String,Object> map = new HashMap<>();
            map.put("playAuth",playAuth);



            return map;

        }catch (Exception e){
            throw new ZhenhuaException(20001,"获取凭证失败");
        }
    }
}